﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;

namespace Aribaccio.Datum
{
    /// <summary>
    /// 数据库连接操作上下文-工厂类
    /// </summary>
    public class CoreDBContextFactory : ICoreDBContextFactory
    {
        /// <summary>
        /// 数据库连接操作对象
        /// </summary>
        protected ICoreDBContext ContextSevice { get; set; }
        /// <summary>
        /// 数据库连接配置对象
        /// </summary>
        protected CoreConnectionOption ConnectionOption { get; set; }

        /// <summary>
        /// 带参构造函数[注入对象]
        /// </summary>
        /// <param name="argCoreDbContext"></param>
        /// <param name="argOptions"></param>
        public CoreDBContextFactory(ICoreDBContext argCoreDbContext, IOptions<CoreConnectionOption> argOptions)
        {
            ContextSevice = argCoreDbContext;
            ConnectionOption = argOptions.Value;
        }
        /// <summary>
        /// 创建数据库连接操作对象
        /// </summary>
        /// <returns>数据库连接操作对象</returns>
        public ICoreDBContext CreateCoreDbContext()
        {
            //给注入的【CoreDbContext】设置连接参数
            ContextSevice.SQLConnectionType = ConnectionOption.ConnectType;
            ContextSevice.SQLConnectionString = ConnectionOption.ConnectionString ?? string.Empty;
            //返回连接操作对象
            return ContextSevice;
        }
    }
}
